/*
 * alesmana
 */

package databeans;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class Employee implements Comparable<Employee> {
    
    //TODO create email entry
    private String username = null;

    private String hashedPassword = "*";
    private int salt = 0;

    private String firstName = null;
    private String lastName = null;
    private String email = null;

    public Employee(String username) {
        this.username = username;
    }

    public boolean checkPassword(String password) {
    	//System.out.println(hashedPassword);
    	//System.out.println(hash(password));
        return hashedPassword.equals(hash(password));
    }

    public int compareTo(Employee other) {
        // Order first by lastName and then by firstName
        int c = lastName.compareTo(other.lastName);
        if (c != 0){
            return c;
        }
        c = firstName.compareTo(other.firstName);
        if (c != 0){
            return c;
        }
        return username.compareTo(other.username);
    }

    public boolean equals(Object obj) {
        if (obj instanceof Employee) {
            Employee other = (Employee) obj;
            return username.equals(other.username);
        }
        return false;
    }

    public String getHashedPassword() {
        return hashedPassword;
    }

    public String getUsername() {
        return username;
    }

    public int getSalt() {
        return salt;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public int hashCode() {
        return username.hashCode();
    }

    public void setHashedPassword(String x) {
        hashedPassword = x;
    }

    public void setPassword(String s) {
        salt = newSalt();
        hashedPassword = hash(s);
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setSalt(int x) {
        salt = x;
    }

    public void setFirstName(String s) {
        firstName = s;
    }

    public void setLastName(String s) {
        lastName = s;
    }

    public void setUsername(String x) {
        username = x;
    }

    // some tostring method
    public String toString() {
        return "Employee-" + getUsername();
    }

    // some random hash function copied from homework
    private String hash(String clearPassword) {
        if (salt == 0)
            return null;

        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA1");
        } catch (NoSuchAlgorithmException e) {
            throw new AssertionError(
                    "Can't find the SHA1 algorithm in the java.security package");
        }

        String saltString = String.valueOf(salt);

        md.update(saltString.getBytes());
        md.update(clearPassword.getBytes());
        byte[] digestBytes = md.digest();

        // Format the digest as a String
        StringBuffer digestSB = new StringBuffer();
        for (int i = 0; i < digestBytes.length; i++) {
            int lowNibble = digestBytes[i] & 0x0f;
            int highNibble = (digestBytes[i] >> 4) & 0x0f;
            digestSB.append(Integer.toHexString(highNibble));
            digestSB.append(Integer.toHexString(lowNibble));
        }
        String digestStr = digestSB.toString();

        return digestStr;
    }

    // things that generate new salt
    private int newSalt() {
        Random random = new Random();
        return random.nextInt(8192) + 1; // salt cannot be zero
    }
}
